<?php
namespace app\admin\controller;
use think\Db;
class Publics extends Base
{
    /**
     * 退出
     */
    public function logOut()
    {
        $adminUser = new \app\admin\model\AdminUser();
        $adminUser->logOut();
        r(url('Publics/logIn'));
    }

    /**
     * 登陆
     */
    public function logIn()
    {
        if (is_post()) {
            $username = input('username');
            $pwd = input('pwd');
            $code = input('code');
            // 验证
            $rule = [
                'username' => 'require',
                'pwd' => 'require',
                'code' => 'require'
            ];
            $msg = [
                'username.require' => '用户名必填',
                'pwd.require' => '密码必填',
                'code.require' => '验证码必填'
            ];
            $data = [
                'username' => $username,
                'pwd' => $pwd,
                'code' => $code
            ];
            com_validate($rule, $msg, $data, true);
            // 验证码
            $v = new \org\Verify();
            if (! $v->check($code,'login')) {
                com_error('验证码错误!','code');
                com_check();
            }
            // 登录
            $adminUser = new \app\admin\model\AdminUser();
            $adminUser->logIn($username, $pwd);
            com_check(['suc_msg'=>'登录成功!','suc_url'=>url('index/index')]);
        }
        $this->view->engine->layout(false);
        return $this->fetch();
    }

    /**
     * 验证码
     */
    public function verify($id='')
    {
        $config = [
            'codeSet' => '123456789',
            'expire' => 1800,
            'fontSize' => 18, // 验证码字体大小(px)
            'useCurve' => false, // 是否画混淆曲线
            'useNoise' => false, // 是否添加杂点
            'imageH' => 40, // 验证码图片高度
            'imageW' => 140, // 验证码图片宽度
            'length' => 4, // 验证码位数
            'fontttf' => '', // 验证码字体，不设置随机获取
            'bg' => [
                243,
                251,
                254
            ], // 背景颜色
            'reset' => true
        ] // 验证成功后是否重置
;
        $verify = new \org\Verify($config);
        return $verify->entry($id);
    }
}
